package com.itzdm.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itzdm.reggie.common.Result.Result;
import com.itzdm.reggie.common.basecontext.BaseContext;
import com.itzdm.reggie.entity.ShoppingCart;
import com.itzdm.reggie.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Api(tags = "购物车相关接口")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("添加购物车：{}",shoppingCart);
        shoppingCartService.saveShoppingCart(shoppingCart);
        return Result.success(shoppingCart);
    }


    /**
     * 查询购物车接口
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车接口")
    public Result<List<ShoppingCart>> getShoppingCart() {
        log.info("查询购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<ShoppingCart>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 去除菜品或套餐
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("去除菜品或套餐")
    public Result<String> delete(@RequestBody ShoppingCart shoppingCart) {
        log.info("减少一件商品：{}",shoppingCart);
        shoppingCartService.sub(shoppingCart);
        return Result.success("减少成功");
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean(){
        log.info("清空购物车");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return Result.success("清除成功");
    }
}
